package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Authority;
import light.head.model.DocumentInfo;
import light.head.model.Organ;
import light.head.model.Role;
import light.head.model.UserInfo;

import org.springframework.stereotype.Service;

@Service
public class DocumentInfoDAO extends BaseDAO<DocumentInfo, Integer>{
	private final static String sql_getByUuid = "FROM DocumentInfo f WHERE f.uuid = ? AND f.deleted = 0";
	private final static String sql_getDc = "FROM Authority WHERE dc = ?";
	/**
	 * 根据查询条件分页查询当前文档的相关消息
	 * @param	当前用户
	 * @param	分页对象
	 * @param	查询条件
	 */
	public RESULT findByMap(List<Integer> ocs,Map<String, String> map, PageBean pageBean) {

		StringBuffer hql = new StringBuffer("FROM DocumentInfo di WHERE 1=1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();

			String dc = map.get(Parm.DC);						//文档类型
			String cu = map.get(Parm.CU);						// 用户名
			String time1 = map.get(Parm.TIME1);						//上传时间
			String time2 = map.get(Parm.TIME2);						//上传时间
			String name = map.get(Parm.NAME);
			String cn = map.get(Parm.CN);
			// 以下是必须的参数，用来查询公开权限是属于自己的文档信息
			String rc = map.get(Parm.RC);						// 权限角色
			String pc = map.get(Parm.PC);						// 权限用户
			String oc = map.get(Parm.OC);						// 权限部门
			
			if (isNotNull(dc)) {
				hql.append(" AND di.dc = ?");
				list.add(Integer.parseInt(dc));
			}if (isNotNull(name)) {
				hql.append(" AND di.name LIKE ?");
				list.add(name);
			}
			if (isNotNull(cn)) {
				hql.append(" AND di.dc IN (SELECT u.code FROM UserInfo u WHERE u.name LIKE ?)");
				list.add(cn);
			}
			if (isNotNull(time1)) {
				hql.append(" AND di.ct >?");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}if (isNotNull(time2)) {
				hql.append(" AND di.ct <?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
			
			if (isNotNull(cu)) {
				hql.append(" AND (di.cu = ?");
				list.add(Integer.parseInt(cu));
				
				if (Integer.parseInt(cu) == Integer.parseInt(pc)) {
					hql.append(" OR ");
				} else {
					hql.append(" AND ");
				}
			} else {
				hql.append(" AND (di.cu = ?");
				list.add(Integer.parseInt(pc));
				hql.append(" OR ");
			}
			hql.append("(di.pType = 1 AND di.oc  IN (");
			for (Integer i : ocs) {
				hql.append("?,");
				list.add(i);
			}
			hql.deleteCharAt(hql.length() - 1).append("))");
			hql.append(" OR di.code IN (SELECT a.dc FROM Authority a WHERE");
			hql.append(" (a.pType = 3");
			hql.append(" OR a.pType = 5");
			if (isNotNull(rc)) {
				hql.append(" OR a.pType = 4)");
			} else {
				hql.append(")");
			}
			
			if (isNotNull(pc) && isNotNull(oc)) {
				hql.append(" AND a.pc IN (?,?");
				list.add(Integer.parseInt(oc));
				list.add(Integer.parseInt(pc));
			}
			
			if (isNotNull(rc)) {
				hql.append(",?)))");
				list.add(Integer.parseInt(rc));
			} else {
				hql.append(")))");
			}
		}
		hql.append(" ORDER BY di.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}

	
	/**
	 * 根据拼接好的机构编号序列获取对应的机构列表
	 */
	public List<Organ> findOrganByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM Organ o WHERE o.deleted = 0  AND o.code IN (" + codes + ")";
		List<Organ> list = find(sql);
		return list;
	}
	
	
	/**
	 * 根据条件分页查询接收机构信息
	 * @param	当前用的机构编号
	 * @param	查询条件
	 * @param	分页对象
	 */
	public RESULT findOrgan(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		StringBuffer hql = new StringBuffer("FROM Organ o WHERE o.deleted = 0 AND o.code IN (");

		List<Object> parm = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");	
			parm.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (null != map) {
			
			String name = map.get(Parm.NAME);
			String suc = map.get(Parm.SUC);
			if (isNotNull(name)) {
				hql.append(" AND o.name LIKE ?");
				parm.add("%" + name + "%");
			}if (isNotNull(suc)) {
				hql.append(" AND o.code NOT IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY o.code DESC");
		return findToPageBean(hql.toString(), pb, parm);
	}
	

	
	/**
	 * 根据拼接好的用户编号序列获取对应的用户列表
	 */
	public List<UserInfo> findUserByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM UserInfo u WHERE u.deleted <> 1 AND u.deleted IS NOT NULL AND u.code IN (" + codes + ")";
		List<UserInfo> list = find(sql);
		return list;
	}
	
	/**
	 * 根据条件分页查询接收人信息
	 * @param	查询条件
	 * @param	分页对象
	 */
	public RESULT findSubUsers(List<Integer> ocs, Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.realName <> 'admin' AND u.deleted <> 1 AND u.deleted IS NOT NULL AND u.oc IN(");
		List<Object> list =  new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String realName = map.get(Parm.REAL_NAME);
			String oc = map.get(Parm.OC);
			String suc = map.get(Parm.SUC);

			if (isNotNull(realName)) {
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(oc)) {
				hql.append(" AND u.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(suc)) {
				hql.append(" AND u.code NOT IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}
	

	/**
	 * 根据拼接好的角色编号序列获取对应的角色列表
	 */
	public List<Role> findRoleByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM Role r WHERE r.deleted <> 1 AND r.deleted IS NOT NULL AND r.code IN (" + codes + ")";
		List<Role> list = find(sql);
		return list;
	}
	/**
	 * 根据条件分页查询接收角色信息
	 * @param	查询条件
	 * @param	分页对象
	 */
	public RESULT findSubRole(List<Integer> ocs, Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Role r WHERE r.deleted <> 1 AND r.deleted IS NOT NULL AND r.oc IN(");
		List<Object> list =  new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String realName = map.get(Parm.REAL_NAME);
			String oc = map.get(Parm.OC);
			String suc = map.get(Parm.SUC);

			if (isNotNull(realName)) {
				hql.append(" AND r.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(oc)) {
				hql.append(" AND r.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(suc)) {
				hql.append(" AND r.code NOT IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY r.oc, r.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}
	

	public DocumentInfo getByUuid(String uuid) {
		if (!isNotNull(uuid)) 
			return null;
		
		List<DocumentInfo> list = find(sql_getByUuid, uuid);
		if (null == list || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	
	public List<DocumentInfo> getByCode(Integer code) {
		if (null == code) return null;
		StringBuffer sql = new StringBuffer("FROM DocumentInfo f WHERE f.deleted = 0 AND f.code = ?");
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		return find(sql.toString(), parm.toArray());
	}
	public List<Authority> getDc(Integer dc) {
		List<Authority> list = find(sql_getDc ,dc);
		return list;
	}
	
	public List<Object> findByCode(Integer code){
		if (null == code) return null;
		StringBuffer sql = new StringBuffer("select a.pc,case d.pType when 3 then o.name when 4 then r.name when 5 then u.realName else '' end as name");
		sql.append(" from Authority a join DocumentInfo d on d.code = a.dc left join Organ o on o.code = a.pc ");
		sql.append(" and d.pType = 3 LEFT JOIN Role r on r.code = a.pc and d.pType = 4 left join UserInfo u on u.code = a.pc and d.pType = 5 WHERE d.code = ?");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(code);
		return query(sql.toString(), null, null, parm.toArray());
	}
	
}

